﻿#region License

// PentaBiz - Sustainable Software Development Framework Copyright (C) 2013 Zoltán Csizmazia
// 
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

#endregion

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using PentaBiz.WpfTools.WeakEvents;

namespace PentaBiz.WpfTools
{
    public static class WeakExtensions
    {
        public static EventHandler<TEventArgs> MakeWeak<TEventArgs>(this EventHandler<TEventArgs> eventHandler,
                                                                    UnregisterEventHandlerCallback<TEventArgs>
                                                                        unregisterEventHandler)
            where TEventArgs : EventArgs
        {
            if (eventHandler == null)
                throw new ArgumentNullException("eventHandler");
            if (eventHandler.Method.IsStatic || eventHandler.Target == null)
                throw new ArgumentException("Only instance methods are supported.", "eventHandler");

            Type wehType =
                typeof (WeakEventHandler<,>).MakeGenericType(eventHandler.Method.DeclaringType, typeof (TEventArgs));

            ConstructorInfo wehConstructor =
                wehType.GetConstructor(new[]
                    {
                        typeof (EventHandler<TEventArgs>),
                        typeof (UnregisterEventHandlerCallback<TEventArgs>)
                    });


            Debug.Assert(wehConstructor != null, "wehConstructor != null");

            var weh = (IWeakEventHandler<TEventArgs>) wehConstructor.Invoke(
                new object[] {eventHandler, unregisterEventHandler});

            return weh.Handler;
        }

        public static PropertyChangedEventHandler MakeWeak(this PropertyChangedEventHandler eventHandler,
                                                           UnregisterPropertyChangedEventHandler
                                                               unregisterEventHandler)
        {
            if (eventHandler == null)
                throw new ArgumentNullException("eventHandler");
            if (eventHandler.Method.IsStatic || eventHandler.Target == null)
                throw new ArgumentException("Only instance methods are supported.", "eventHandler");

            Type wehType =
                typeof (WeakPropertyChangedEventHandler<>).MakeGenericType(eventHandler.Method.DeclaringType);

            ConstructorInfo wehConstructor =
                wehType.GetConstructor(new[]
                    {
                        typeof (PropertyChangedEventHandler),
                        typeof (UnregisterPropertyChangedEventHandler)
                    });


            Debug.Assert(wehConstructor != null, "wehConstructor != null");

            var weh = (IWeakPropertyChangedEventHandler) wehConstructor.Invoke(
                new object[] {eventHandler, unregisterEventHandler});

            return weh.Handler;
        }
    }
}